import xarray as xr
import numpy as np
import netCDF4 as nc
from scipy.stats.mstats import ttest_ind
import matplotlib as mpl
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cartopy.mpl.ticker as cticker
import cartopy.io.shapereader as shpreader
import matplotlib.pyplot as plt
from cartopy.util import add_cyclic_point
from scipy.interpolate import interp2d
from scipy import interpolate

treefrac2 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126/treeFrac_Lmon_CanESM5_ssp126_r1i1p2f1_gn_201501-210012.nc')
treefrac22 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126-ssp370/treeFrac_Lmon_CanESM5_ssp126-ssp370Lu_r1i1p2f1_gn_201501-210012.nc')
treefrac1 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126/treeFrac_Lmon_ACCESS-ESM1-5_ssp126_r1i1p1f1_gn_201501-210012.nc')
treefrac11 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126-ssp370/treeFrac_Lmon_ACCESS-ESM1-5_ssp126-ssp370Lu_r1i1p1f1_gn_201501-210012.nc')
treefrac3 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126/treeFrac_Lmon_CMCC-ESM2_ssp126_r1i1p1f1_gn_201501-210012.nc')
treefrac33 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126-ssp370/treeFrac_Lmon_CMCC-ESM2_ssp126-ssp370Lu_r1i1p1f1_gn_201501-210012.nc')
treefrac4 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126/treeFrac_Lmon_IPSL-CM6A-LR_ssp126_r1i1p1f1_gr_201501-210012.nc')
treefrac44 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126-ssp370/treeFrac_Lmon_IPSL-CM6A-LR_ssp126-ssp370Lu_r1i1p1f1_gr_201501-210012.nc')
treefrac5 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126/treeFrac_Lmon_MPI-ESM1-2-LR_ssp126_r1i1p1f1_gn_201501-210012.nc')
treefrac55 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126-ssp370/treeFrac_Lmon_MPI-ESM1-2-LR_ssp126-ssp370Lu_r1i1p1f1_gn_201501-210012.nc')
treefrac6 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126/treeFrac_Lmon_GFDL-ESM4_ssp126_r1i1p1f1_gr1_201501-210012.nc')
treefrac66 = xr.open_dataset('D:/Python/XXHH/treeFrac-ssp126-ssp370/treeFrac_Lmon_GFDL-ESM4_ssp126-ssp370Lu_r1i1p1f1_gr1_201501-210012.nc')
print(treefrac1)
treeFrac1 = treefrac1.treeFrac.loc['2015-01-01':'2100-12-16', :, :]
treeFrac2 = treefrac2.treeFrac.loc['2015-01-01':'2100-12-16', :, :]
treeFrac3 = treefrac3.treeFrac.loc['2015-01-01':'2100-12-16', :, :]
treeFrac4 = treefrac4.treeFrac.loc['2015-01-01':'2100-12-16', :, :]
treeFrac5 = treefrac5['treeFrac']
treeFrac6 = treefrac6.treeFrac.loc['2015-01-01':'2100-12-16', :, :]
treeFrac11 = treefrac11.treeFrac.loc['2015-01-01':'2100-12-16', :, :]
treeFrac22 = treefrac22.treeFrac.loc['2015-01-01':'2100-12-16', :, :]
treeFrac33 = treefrac33.treeFrac.loc['2015-01-01':'2100-12-16', :, :]
treeFrac44 = treefrac44.treeFrac.loc['2015-01-01':'2100-12-16', :, :]
treeFrac55 = treefrac55['treeFrac']
treeFrac66 = treefrac66.treeFrac.loc['2015-01-01':'2100-12-16', :, :]

print(treeFrac1.shape)
print(treeFrac5.shape)
print(treeFrac55)

print(treeFrac5.shape)

lon1 = treeFrac1.lon
print(lon1)
lat1 = treeFrac1.lat
print(lat1)
lon2 = treeFrac2.lon
lat2 = treeFrac2.lat
lon3 = treeFrac3.lon
lat3 = treeFrac3.lat
lon4 = treeFrac4.lon
lat4 = treeFrac4.lat
lon5 = treeFrac5.lon
lat5 = treeFrac5.lat
lon6 = treeFrac6.lon
lat6 = treeFrac6.lat


t1,p1= ttest_ind(treeFrac11,treeFrac1,equal_var=False)
t2,p2= ttest_ind(treeFrac22,treeFrac2,equal_var=False)
t3,p3= ttest_ind(treeFrac33,treeFrac3,equal_var=False)
t4,p4= ttest_ind(treeFrac44,treeFrac4,equal_var=False)
t5,p5= ttest_ind(treeFrac55,treeFrac5,equal_var=False)
t6,p6= ttest_ind(treeFrac66,treeFrac6,equal_var=False)
print(np.min(t1),np.max(t1))
print(np.min(p1),np.max(p1))
print(p1)

treeFrac1 = np.nanmean(treeFrac1, axis=0)
print(treeFrac1.shape)
treeFrac2 = np.nanmean(treeFrac2, axis=0)
treeFrac3 = np.nanmean(treeFrac3, axis=0)
treeFrac4 = np.nanmean(treeFrac4, axis=0)
treeFrac5 = np.nanmean(treeFrac5, axis=0)
treeFrac6 = np.nanmean(treeFrac6, axis=0)
treeFrac1[treeFrac1<50]=np.nan
treeFrac2[treeFrac2<50]=np.nan
treeFrac3[treeFrac3<50]=np.nan
treeFrac4[treeFrac4<50]=np.nan
treeFrac5[treeFrac5<50]=np.nan
treeFrac6[treeFrac6<50]=np.nan

treeFrac11 = np.nanmean(treeFrac11, axis=0)
print(treeFrac1.shape)
treeFrac22 = np.nanmean(treeFrac22, axis=0)
treeFrac33 = np.nanmean(treeFrac33, axis=0)
treeFrac44 = np.nanmean(treeFrac44, axis=0)
treeFrac55 = np.nanmean(treeFrac55, axis=0)
treeFrac66 = np.nanmean(treeFrac66, axis=0)


tree11 = treeFrac11 - treeFrac1
tree22 = treeFrac22 - treeFrac2
tree33 = treeFrac33 - treeFrac3
tree44 = treeFrac44 - treeFrac4
tree55 = treeFrac55 - treeFrac5
tree66 = treeFrac66 - treeFrac6
print(tree11.shape)
print(tree22.shape)
print(tree33.shape)
print(tree44.shape)
print(tree55.shape)
print(tree66.shape)

tas_mean1 = np.where(np.isnan(tree11), 0, tree11)
tas_mean2 = np.where(np.isnan(tree22), 0, tree22)
tas_mean3 = np.where(np.isnan(tree33), 0, tree33)
tas_mean4 = np.where(np.isnan(tree44), 0, tree44)
tas_mean5 = np.where(np.isnan(tree55), 0, tree55)
tas_mean6 = np.where(np.isnan(tree66), 0, tree66)


lon1 = np.array(lon1)
lat1 = np.array(lat1)
lon2 = np.array(lon2)
lat2 = np.array(lat2)
lon3 = np.array(lon3)
lat3 = np.array(lat3)
lon4 = np.array(lon4)
lat4 = np.array(lat4)
lon5 = np.array(lon5)
lat5 = np.array(lat5)

lon_new = np.arange(0,360,1)
lat_new = np.arange(-90,90,1)

f1 = interpolate.interp2d(lon1, lat1, tas_mean1, kind='linear')
tas_mean1_new = f1(lon_new, lat_new)

f2 = interp2d(lon2, lat2, tas_mean2, kind='linear')
tas_mean2_new = f2(lon_new, lat_new)


f3 = interp2d(lon3, lat3, tas_mean3, kind='linear')
tas_mean3_new = f3(lon_new, lat_new)

f4 = interp2d(lon4, lat4, tas_mean4, kind='linear')
tas_mean4_new = f4(lon_new, lat_new)

f5 = interp2d(lon5, lat5, tas_mean5, kind='linear')
tas_mean5_new = f5(lon_new, lat_new)




cycle_sst1, cycle_lon1 = add_cyclic_point(treeFrac1, coord=lon1)
cycle_LON1, cycle_LAT1 = np.meshgrid(cycle_lon1, lat1)
cycle_sst2, cycle_lon2 = add_cyclic_point(treeFrac2, coord=lon2)
cycle_LON2, cycle_LAT2 = np.meshgrid(cycle_lon2, lat2)
cycle_sst3, cycle_lon3 = add_cyclic_point(treeFrac3, coord=lon3)
cycle_LON3, cycle_LAT3 = np.meshgrid(cycle_lon3, lat3)
cycle_sst4, cycle_lon4 = add_cyclic_point(treeFrac4, coord=lon4)
cycle_LON4, cycle_LAT4 = np.meshgrid(cycle_lon4, lat4)
cycle_sst5, cycle_lon5 = add_cyclic_point(treeFrac5, coord=lon5)
cycle_LON5, cycle_LAT5 = np.meshgrid(cycle_lon5, lat5)
cycle_sst6, cycle_lon6 = add_cyclic_point(treeFrac6, coord=lon6)
cycle_LON6, cycle_LAT6 = np.meshgrid(cycle_lon6, lat6)

cycle_sst11, cycle_lon1 = add_cyclic_point(treeFrac11, coord=lon1)
cycle_LON1, cycle_LAT1 = np.meshgrid(cycle_lon1, lat1)
cycle_sst22, cycle_lon2 = add_cyclic_point(treeFrac22, coord=lon2)
cycle_LON2, cycle_LAT2 = np.meshgrid(cycle_lon2, lat2)
cycle_sst33, cycle_lon3 = add_cyclic_point(treeFrac33, coord=lon3)
cycle_LON3, cycle_LAT3 = np.meshgrid(cycle_lon3, lat3)
cycle_sst44, cycle_lon4 = add_cyclic_point(treeFrac44, coord=lon4)
cycle_LON4, cycle_LAT4 = np.meshgrid(cycle_lon4, lat4)
cycle_sst55, cycle_lon5 = add_cyclic_point(treeFrac55, coord=lon5)
cycle_LON5, cycle_LAT5 = np.meshgrid(cycle_lon5, lat5)
cycle_sst66, cycle_lon6 = add_cyclic_point(treeFrac66, coord=lon6)
cycle_LON6, cycle_LAT6 = np.meshgrid(cycle_lon6, lat6)

tree11 = cycle_sst11 - cycle_sst1
tree22 = cycle_sst22 - cycle_sst2
tree33 = cycle_sst33 - cycle_sst3
tree44 = cycle_sst44 - cycle_sst4
tree55 = cycle_sst55 - cycle_sst5
tree66 = cycle_sst66 - cycle_sst6
print(tree11.shape)
print(tree22.shape)
print(tree33.shape)
print(tree44.shape)
print(tree55.shape)
print(tree66.shape)


tree11[tree11>=0]=np.nan
tree22[tree22>=0]=np.nan
tree33[tree33>=0]=np.nan
tree44[tree44>=0]=np.nan
tree55[tree55>=0]=np.nan
tree66[tree66>=0]=np.nan


print(2)
print(cycle_lon1)
f_g = nc.Dataset('D:/Python/XXHH/deforestion_GFDL-ESM4.nc','w',format = 'NETCDF4')


f_g.createDimension('lat',len(lat6))
f_g.createDimension('lon',len(cycle_lon6))


##创建变量。参数依次为：‘变量名称’，‘数据类型’，‘基础维度信息’

f_g.createVariable('lat',np.float32,('lat'))
f_g.createVariable('lon',np.float32,('lon'))
f_g.createVariable('treefrac', np.float32, ('lat','lon'))
#f_g.createVariable('GPP', np.float32, ('lat','lon'))


lon = np.array(cycle_lon6)
print(lon.shape)
f_g.variables['lon'][:] = lon
lat = np.array(lat6)
f_g.variables['lat'][:] = lat
var_data = tree66
f_g.variables['treefrac'][:, :] = var_data

f_LR1 = xr.open_dataset('D:/Python/XXHH/deforestion_GFDL-ESM4.nc')
print(f_LR1)

proj = ccrs.PlateCarree(central_longitude=0)
#leftlon, rightlon, lowerlat, upperlat = (-180,180,-90,90)
leftlon, rightlon, lowerlat, upperlat = (0,361,-90,90)
img_extent = [leftlon, rightlon, lowerlat, upperlat]
fig = plt.figure(figsize=(12,8))

ax1 = fig.add_axes([0.05, 0.7, 0.22, 0.32],projection = proj)
#ax1.set_title('(a)',loc='left',fontsize=22,family='Times New Roman')
ax1.set_title('(a) ACCESS-ESM1-5',loc='left',fontsize=11,family='Times New Roman')
ax1.set_extent(img_extent, crs=ccrs.PlateCarree())
ax1.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax1.add_feature(cfeature.LAKES, alpha=0.5)
#ax1.set_xticks(np.array([-180,-135,-90,-45,0,45,90,135,180]), crs=ccrs.PlateCarree())
ax1.set_xticks(np.array([-180,-120,-60,0,60,120,180]), crs=ccrs.PlateCarree())
ax1.set_yticks(np.array([-90,-60,-30,0,30,60,90]), crs=ccrs.PlateCarree())
ax1.xaxis.set_major_formatter(cticker.LongitudeFormatter())
ax1.yaxis.set_major_formatter(cticker.LatitudeFormatter())
plt.xticks(fontsize=9,family='Times New Roman')
plt.yticks(fontsize=9,family='Times New Roman')
cmap = mpl.cm.YlOrBr_r
#clist=['orange','honeydew','lightgreen','limegreen','mediumseagreen','green','forestgreen','seagreen', 'darkgreen']
# N 表示插值后的颜色数目
#newcmp = LinearSegmentedColormap.from_list('chaos',clist,N=256)
newcolors=cmap(np.linspace(0, 1, 256))
newcmp = ListedColormap(newcolors[0:200])
ax1.add_feature(cfeature.NaturalEarthFeature('physical', 'ocean', '110m', edgecolor='black', facecolor='white'))
#levels = (0,50,100,125,150,175, 200,225,250,275,300, 600)
#c1 = ax.contourf(X, Y, BPRE, zorder=0, extend = 'both',levels=levels, transform=ccrs.PlateCarree(), cmap=newcmp)
c1 = ax1.contourf(cycle_LON1, cycle_LAT1, tree11, zorder=0, extend = 'both',transform=ccrs.PlateCarree(),levels=np.arange(-10,0,1),cmap=newcmp)
#c2p = ax1.contourf(cycle_LON1, cycle_LAT1, cycle_p11, levels=[0, 0.05, 1], hatches=['.....', None], zorder=1, colors="none",transform=ccrs.PlateCarree())
#c2p = ax1.contourf(cycle_LON1, cycle_LAT1, cycle_t11, levels=[0, 1.96, 249.45659], hatches=[None,'...'], zorder=0, colors="none",transform=ccrs.PlateCarree())
#cb = plt.colorbar(c1, orientation='horizontal',fraction=0.05)
#ax1.add_geometries(china, ccrs.PlateCarree(), facecolor='none', edgecolor='k', linewidth=1, zorder=1)

ax2 = fig.add_axes([0.05, 0.40, 0.22, 0.32],projection = proj)
#ax1.set_title('(a)',loc='left',fontsize=22,family='Times New Roman')
ax2.set_title('(c) CanESM5',loc='left',fontsize=11,family='Times New Roman')
ax2.set_extent(img_extent, crs=ccrs.PlateCarree())
ax2.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax2.add_feature(cfeature.LAKES, alpha=0.5)
#ax1.set_xticks(np.array([-180,-135,-90,-45,0,45,90,135,180]), crs=ccrs.PlateCarree())
ax2.set_xticks(np.array([-180,-120,-60,0,60,120,180]), crs=ccrs.PlateCarree())
ax2.set_yticks(np.array([-90,-60,-30,0,30,60,90]), crs=ccrs.PlateCarree())
ax2.xaxis.set_major_formatter(cticker.LongitudeFormatter())
ax2.yaxis.set_major_formatter(cticker.LatitudeFormatter())
plt.xticks(fontsize=9,family='Times New Roman')
plt.yticks(fontsize=9,family='Times New Roman')
cmap = mpl.cm.YlOrBr_r
#clist=['orange','honeydew','lightgreen','limegreen','mediumseagreen','green','forestgreen','seagreen', 'darkgreen']
# N 表示插值后的颜色数目
#newcmp = LinearSegmentedColormap.from_list('chaos',clist,N=256)
newcolors=cmap(np.linspace(0, 1, 256))
newcmp = ListedColormap(newcolors[0:200])
ax2.add_feature(cfeature.NaturalEarthFeature('physical', 'ocean', '110m', edgecolor='black', facecolor='white'))
#levels = (0,50,100,125,150,175, 200,225,250,275,300, 600)
#c1 = ax.contourf(X, Y, BPRE, zorder=0, extend = 'both',levels=levels, transform=ccrs.PlateCarree(), cmap=newcmp)
c2 = ax2.contourf(cycle_LON2, cycle_LAT2, tree22, zorder=0, extend = 'both',transform=ccrs.PlateCarree(),levels=np.arange(-10,0,1),cmap=newcmp)
#c2p = ax2.contourf(cycle_LON2, cycle_LAT2, cycle_p22, levels=[0, 0.05, 1], hatches=['...', None], zorder=1, colors="none",transform=ccrs.PlateCarree())
#ax1.add_geometries(china, ccrs.PlateCarree(), facecolor='none', edgecolor='k', linewidth=1, zorder=1)
#cb = plt.colorbar(c2, orientation='horizontal',fraction=0.05)

ax3 = fig.add_axes([0.05, 0.10, 0.22, 0.32],projection = proj)
#ax1.set_title('(a)',loc='left',fontsize=22,family='Times New Roman')
ax3.set_title('(e) CMCC-ESM2',loc='left',fontsize=11,family='Times New Roman')
ax3.set_extent(img_extent, crs=ccrs.PlateCarree())
ax3.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax3.add_feature(cfeature.LAKES, alpha=0.5)
#ax1.set_xticks(np.array([-180,-135,-90,-45,0,45,90,135,180]), crs=ccrs.PlateCarree())
ax3.set_xticks(np.array([-180,-120,-60,0,60,120,180]), crs=ccrs.PlateCarree())
ax3.set_yticks(np.array([-90,-60,-30,0,30,60,90]), crs=ccrs.PlateCarree())
ax3.xaxis.set_major_formatter(cticker.LongitudeFormatter())
ax3.yaxis.set_major_formatter(cticker.LatitudeFormatter())
plt.xticks(fontsize=9,family='Times New Roman')
plt.yticks(fontsize=9,family='Times New Roman')
cmap = mpl.cm.YlOrBr_r
#clist=['orange','honeydew','lightgreen','limegreen','mediumseagreen','green','forestgreen','seagreen', 'darkgreen']
# N 表示插值后的颜色数目
#newcmp = LinearSegmentedColormap.from_list('chaos',clist,N=256)
newcolors=cmap(np.linspace(0, 1, 256))
newcmp = ListedColormap(newcolors[0:200])
ax3.add_feature(cfeature.NaturalEarthFeature('physical', 'ocean', '110m', edgecolor='black', facecolor='white'))
#levels = (0,50,100,125,150,175, 200,225,250,275,300, 600)
#c1 = ax.contourf(X, Y, BPRE, zorder=0, extend = 'both',levels=levels, transform=ccrs.PlateCarree(), cmap=newcmp)
c3 = ax3.contourf(cycle_LON3, cycle_LAT3, tree33, zorder=0, extend = 'both',transform=ccrs.PlateCarree(),levels=np.arange(-10,0,1),cmap=newcmp)
#c2p = ax3.contourf(cycle_LON3, cycle_LAT3, cycle_p33, levels=[0, 0.05, 1], hatches=['...', None], zorder=1, colors="none",transform=ccrs.PlateCarree())
#ax1.add_geometries(china, ccrs.PlateCarree(), facecolor='none', edgecolor='k', linewidth=1, zorder=1)
#cb = plt.colorbar(c3, orientation='horizontal',fraction=0.05)

ax4 = fig.add_axes([0.32, 0.70, 0.22, 0.32],projection = proj)
#ax1.set_title('(a)',loc='left',fontsize=22,family='Times New Roman')
ax4.set_title('(b) IPSL-CM6A-LR',loc='left',fontsize=11,family='Times New Roman')
ax4.set_extent(img_extent, crs=ccrs.PlateCarree())
ax4.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax4.add_feature(cfeature.LAKES, alpha=0.5)
#ax1.set_xticks(np.array([-180,-135,-90,-45,0,45,90,135,180]), crs=ccrs.PlateCarree())
ax4.set_xticks(np.array([-180,-120,-60,0,60,120,180]), crs=ccrs.PlateCarree())
ax4.set_yticks(np.array([-90,-60,-30,0,30,60,90]), crs=ccrs.PlateCarree())
ax4.xaxis.set_major_formatter(cticker.LongitudeFormatter())
ax4.yaxis.set_major_formatter(cticker.LatitudeFormatter())
plt.xticks(fontsize=9,family='Times New Roman')
plt.yticks(fontsize=9,family='Times New Roman')
cmap = mpl.cm.YlOrBr_r
#clist=['orange','honeydew','lightgreen','limegreen','mediumseagreen','green','forestgreen','seagreen', 'darkgreen']
# N 表示插值后的颜色数目
#newcmp = LinearSegmentedColormap.from_list('chaos',clist,N=256)
newcolors=cmap(np.linspace(0, 1, 256))
newcmp = ListedColormap(newcolors[0:200])
ax4.add_feature(cfeature.NaturalEarthFeature('physical', 'ocean', '110m', edgecolor='black', facecolor='white'))
#levels = (0,50,100,125,150,175, 200,225,250,275,300, 600)
#c1 = ax.contourf(X, Y, BPRE, zorder=0, extend = 'both',levels=levels, transform=ccrs.PlateCarree(), cmap=newcmp)
c4 = ax4.contourf(cycle_LON4, cycle_LAT4, tree44, zorder=0, extend = 'both',transform=ccrs.PlateCarree(),levels=np.arange(-10,0,1),cmap=newcmp)
#c2p = ax4.contourf(cycle_LON4, cycle_LAT4, cycle_p44, levels=[0, 0.05, 1], hatches=['...', None], zorder=1, colors="none",transform=ccrs.PlateCarree())
#ax1.add_geometries(china, ccrs.PlateCarree(), facecolor='none', edgecolor='k', linewidth=1, zorder=1)
#cb = plt.colorbar(c4, orientation='horizontal',fraction=0.05)
#cb.set_label('Tree Fraction(%)', fontsize=12,family='Times New Roman')
#cb = fig.colorbar(c4, ax=[ax1, ax2, ax3, ax4],  shrink=1, orientation='horizontal',pad=0.04)
#cb.set_label('Tree Fraction Changes(%)', fontsize=10,family='Times New Roman')

ax5 = fig.add_axes([0.32, 0.4, 0.22, 0.32],projection = proj)
#ax1.set_title('(a)',loc='left',fontsize=22,family='Times New Roman')
ax5.set_title('(d) MPI-ESM1-2-LR',loc='left',fontsize=11,family='Times New Roman')
ax5.set_extent(img_extent, crs=ccrs.PlateCarree())
ax5.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax5.add_feature(cfeature.LAKES, alpha=0.5)
#ax1.set_xticks(np.array([-180,-135,-90,-45,0,45,90,135,180]), crs=ccrs.PlateCarree())
ax5.set_xticks(np.array([-180,-120,-60,0,60,120,180]), crs=ccrs.PlateCarree())
ax5.set_yticks(np.array([-90,-60,-30,0,30,60,90]), crs=ccrs.PlateCarree())
ax5.xaxis.set_major_formatter(cticker.LongitudeFormatter())
ax5.yaxis.set_major_formatter(cticker.LatitudeFormatter())
plt.xticks(fontsize=9,family='Times New Roman')
plt.yticks(fontsize=9,family='Times New Roman')
cmap = mpl.cm.YlOrBr_r
#clist=['orange','honeydew','lightgreen','limegreen','mediumseagreen','green','forestgreen','seagreen', 'darkgreen']
# N 表示插值后的颜色数目
#newcmp = LinearSegmentedColormap.from_list('chaos',clist,N=256)
newcolors=cmap(np.linspace(0, 1, 256))
newcmp = ListedColormap(newcolors[0:200])
ax5.add_feature(cfeature.NaturalEarthFeature('physical', 'ocean', '110m', edgecolor='black', facecolor='white'))
#levels = (0,50,100,125,150,175, 200,225,250,275,300, 600)
#c1 = ax.contourf(X, Y, BPRE, zorder=0, extend = 'both',levels=levels, transform=ccrs.PlateCarree(), cmap=newcmp)
c5 = ax5.contourf(cycle_LON5, cycle_LAT5, tree55, zorder=0, extend = 'both',transform=ccrs.PlateCarree(),levels=np.arange(-10,0,1),cmap=newcmp)
#c2p = ax5.contourf(cycle_LON5, cycle_LAT5, cycle_p55, levels=[0, 0.05, 1], hatches=['...', None], zorder=1, colors="none",transform=ccrs.PlateCarree())
#ax1.add_geometries(china, ccrs.PlateCarree(), facecolor='none', edgecolor='k', linewidth=1, zorder=1)
#cb = plt.colorbar(c5, orientation='horizontal',fraction=0.05)
#cb.set_label('Tree Fraction(%)', fontsize=12,family='Times New Roman')
#cb = fig.colorbar(c4, ax=[ax1, ax2, ax3, ax4],  shrink=1, orientation='horizontal',pad=0.04)
#cb.set_label('Tree Fraction(%)', fontsize=10,family='Times New Roman')

ax6 = fig.add_axes([0.32, 0.10, 0.22, 0.32],projection = proj)
#ax1.set_title('(a)',loc='left',fontsize=22,family='Times New Roman')
ax6.set_title('(f) GFDL-ESM4',loc='left',fontsize=11,family='Times New Roman')
ax6.set_extent(img_extent, crs=ccrs.PlateCarree())
ax6.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax6.add_feature(cfeature.LAKES, alpha=0.5)
#ax1.set_xticks(np.array([-180,-135,-90,-45,0,45,90,135,180]), crs=ccrs.PlateCarree())
ax6.set_xticks(np.array([-180,-120,-60,0,60,120,180]), crs=ccrs.PlateCarree())
ax6.set_yticks(np.array([-90,-60,-30,0,30,60,90]), crs=ccrs.PlateCarree())
ax6.xaxis.set_major_formatter(cticker.LongitudeFormatter())
ax6.yaxis.set_major_formatter(cticker.LatitudeFormatter())
plt.xticks(fontsize=9,family='Times New Roman')
plt.yticks(fontsize=9,family='Times New Roman')
cmap = mpl.cm.YlOrBr_r
#clist=['orange','honeydew','lightgreen','limegreen','mediumseagreen','green','forestgreen','seagreen', 'darkgreen']
# N 表示插值后的颜色数目
#newcmp = LinearSegmentedColormap.from_list('chaos',clist,N=256)
newcolors=cmap(np.linspace(0, 1, 256))
newcmp = ListedColormap(newcolors[0:200])
ax6.add_feature(cfeature.NaturalEarthFeature('physical', 'ocean', '110m', edgecolor='black', facecolor='white'))
#levels = (0,50,100,125,150,175, 200,225,250,275,300, 600)
#c1 = ax.contourf(X, Y, BPRE, zorder=0, extend = 'both',levels=levels, transform=ccrs.PlateCarree(), cmap=newcmp)
c6 = ax6.contourf(cycle_LON6, cycle_LAT6, tree66, zorder=0, extend = 'both',transform=ccrs.PlateCarree(),levels=np.arange(-10,0,1),cmap=newcmp)
#c2p = ax6.contourf(cycle_LON6, cycle_LAT6, cycle_p66, levels=[0, 0.05, 1], hatches=['...', None], zorder=1, colors="none",transform=ccrs.PlateCarree())
#ax1.add_geometries(china, ccrs.PlateCarree(), facecolor='none', edgecolor='k', linewidth=1, zorder=1)
#cb = plt.colorbar(c6, orientation='horizontal',fraction=0.05)
#cb.set_label('Tree Fraction(%)', fontsize=12,family='Times New Roman')
cb = fig.colorbar(c6, ax=[ax1, ax2, ax3, ax4, ax5, ax6],  shrink=0.5, orientation='horizontal',pad=0.07)
cb.set_label('Tree Fraction(%)', fontsize=10,family='Times New Roman')
#plt.savefig('D:/picture/second/figs1.pdf',dpi=400,bbox_inches='tight')
#plt.savefig('D:/picture/second/fig 50%.tif',dpi=400,bbox_inches='tight')
plt.show()
